package com.chuhe.starry.dao;

import com.chuhe.common.domain.entity.UserContactApply;
import com.chuhe.common.domain.enums.PageSize;
import com.chuhe.common.domain.enums.UserContactApplyStatusEnum;
import com.chuhe.common.domain.query.PaginationResultVO;
import com.chuhe.common.domain.query.SimplePage;
import com.chuhe.common.domain.vo.resp.LoadApplyResp;
import com.chuhe.starry.mapper.UserContactApplyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 联系人申请 服务实现类
 * </p>
 *
 * @author 张楚河
 * @since 2025-05-16
 */
@Service
public class UserContactApplyDao extends ServiceImpl<UserContactApplyMapper, UserContactApply> {
    /**
     * 查询申请记录
     * @Author: 楚河
     * @Date: 2025-05-16 17:18
     */
    public UserContactApply selectByApplyUserIdAndReceiveUserIdAndContactId(String userId, String receiveUserId, String contactId) {
        return lambdaQuery()
                .eq(UserContactApply::getApplyUserId, userId)
                .eq(UserContactApply::getReceiveUserId, receiveUserId)
                .eq(UserContactApply::getContactId, contactId)
                .one();
    }
    /**
     * 更新申请记录
     * @Author: 楚河
     * @Date: 2025-05-16 17:28
     */
    public void updateContactApply(UserContactApply userContactApply, Integer applyId) {
        lambdaUpdate()
                .eq(UserContactApply::getApplyId,applyId)
                .set(UserContactApply::getStatus,userContactApply.getStatus())
                .set(UserContactApply::getLastApplyTime,userContactApply.getLastApplyTime())
                .set(UserContactApply::getApplyInfo,userContactApply.getApplyInfo())
                .update();
    }
    /**
     * 加载联系人申请列表
     * @Author: 楚河
     * @Date: 2025-05-16 18:51
     */
    public PaginationResultVO<LoadApplyResp> findApplyListByPage(LoadApplyResp userContactApply) {
        //调用方法 → 获取总记录数 → 处理分页参数 → 查询当前页数据 → 封装结果返回
        Integer count = lambdaQuery()
                .eq(UserContactApply::getReceiveUserId, userContactApply.getReceiveUserId())
                .count();
        int pageSize = userContactApply.getPageSize() == null ? PageSize.SIZE15.getSize() : userContactApply.getPageSize();

        SimplePage page = new SimplePage(userContactApply.getPageNo(), count, pageSize);
        userContactApply.setSimplePage(page);

        List<UserContactApply> list = lambdaQuery()
                .eq(UserContactApply::getReceiveUserId, userContactApply.getReceiveUserId())
                .list();

        PaginationResultVO<LoadApplyResp> resultVO = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), list);
        return resultVO;
    }
    /**
     * 查询记录表根据主键id
     * @Author: 楚河
     * @Date: 2025-05-16 20:19
     */
    public UserContactApply selectByApplyId(Integer applyId) {
        return lambdaQuery()
                .eq(UserContactApply::getApplyId, applyId)
                .one();
    }
    /**
     * 查询待处理申请数量
     * @Author: 楚河
     * @Date: 2025-05-16 20:29
     */
    public Integer selecyInitCount(Integer applyId, Integer status) {
        return lambdaQuery()
                .eq(UserContactApply::getApplyId, applyId)
                .eq(UserContactApply::getStatus, status)
                .count();
    }
    /**
     * 修改记录表
     * @Author: 楚河
     * @Date: 2025-05-16 21:10
     */
    public void updateApplyInfo(UserContactApply userContactApply) {
        lambdaUpdate()
                .eq(UserContactApply::getApplyId, userContactApply.getApplyId())
                .set(UserContactApply::getStatus, userContactApply.getStatus())
                .set(UserContactApply::getLastApplyTime,userContactApply.getLastApplyTime())
                .update();
    }
    /**
     * 查询新申请数量
     * @Author: 楚河
     * @Date: 2025-05-21 22:49
     */
    public Integer selectCount(String userId) {
        return lambdaQuery()
                .eq(UserContactApply::getReceiveUserId, userId)
                .eq(UserContactApply::getStatus, UserContactApplyStatusEnum.INIT.getStatus())
                .count();


    }
}
